﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace World
{
    /// <summary>
    /// Handles all events
    /// </summary>
    public static class EventHandler
    {
        #region property


        #endregion

        #region methods

       


        /// <summary>
        /// Perform action associated with certain event
        /// </summary>
        /// <param name="Event"></param>
        public static void RaiseEvent(Util.Classes.Event Event)
        {
            try
            {
                //first, what type of event
                switch (Event.EventType)
                {
                    case Util.Helpers.PublicEnums.EventType.onAvatarSpotted:
                        {
                            //switch method name
                            #region what method to call

                            switch (Event.Method)
                            {
                                case "CallBackup":
                                    {
                                        //call the method
                                        Levels.LevelManager.CallBackup((Characters.NPC)Event.Parameters[0].Parameter);

                                        break;
                                    }
                                case "KillAvatar":
                                    {
                                        //call the method
                                        Levels.LevelManager.KillAvatar((Characters.NPC)Event.Parameters[0].Parameter);

                                        break;
                                    }
                                case "BeatAvatar":
                                    {
                                        //call the method
                                        Levels.LevelManager.KillAvatar((Characters.NPC)Event.Parameters[0].Parameter);

                                        break;
                                    }
                                   

                            }
                            #endregion



                            break;
                        }
                    case Util.Helpers.PublicEnums.EventType.onDefuseFailed:
                        {
                            switch (Event.Method)
                            {
                                case "DefuseFailed":
                                    {
                                        World.WorldManager.IsDefusingBomb = false; //no more defusing
                                        World.WorldManager.BombInstanceBeingDefused = null;
                                        World.WorldManager.BombBeingDefused = null;
                                        //EXPLOSION
                                        World.WorldManager.MakeAnExplosion();


                                        break;
                                    }


                            }

                            break;
                        }
                    case Util.Helpers.PublicEnums.EventType.OnInteraction:
                        {
                            switch (Event.Method)
                            {
                                case "InteractWithItem":
                                    {
                                        World.WorldManager.ItemInteractsWithObstacle((Util.Classes.ObstacleToDraw)Event.Parameters[0].Parameter, (Util.Classes.Item)Event.Parameters[1].Parameter);


                                        break;
                                    }
                            }


                            break;
                        }
                    case Util.Helpers.PublicEnums.EventType.onLandmineStepped:
                        {
                            switch (Event.Method)
                            {
                                case "LandmineTrigger":
                                    {
                                        //remember that there can be multiple obstacles to draw of the same type on the map cell
                                        Util.Classes.ObstacleToDraw otd=(Util.Classes.ObstacleToDraw)Event.Parameters[0].Parameter;

                                        //avatar stepped on the landmine
                                        //1. hurt avatar
                                        Util.Cache.Avatar.HurtAvatar(Levels.LevelManager.GetObstacleByName(otd.ObstacleID).HarmMultiplier);



                                        //2. set current state to 2 for the obstacle landmine so from now on, there is only a disarmed landmine
                                        World.WorldManager.ItemInteractsWithObstacle(otd, null);

                                        //otd.CurrentState = 1;
                                        //otd.ObstacleID = otd.ItemState2;//change names, landmine becomes landmineDisarmed
                                        //otd.State2Obstacle = null;
                                        //otd.ItemState2 = null;
                                        //otd.HasState2 = false;
                                        //otd.TextureSet = false;

                                        //3. show particle effect=explosion (small one)

                                        break;
                                    }
                            }
                         


                            break;
                        }
                    case Util.Helpers.PublicEnums.EventType.onItemCollected:
                        {
                            switch (Event.Method)
                            {
                                case "AddItemToInventory":
                                    {
                                        Util.Cache.Avatar.AddItemToTheInventory((Util.Classes.Item)Event.Parameters[1].Parameter, (int)Event.Parameters[0].Parameter);

                                        break;
                                    }
                                case "AddEnergy":
                                    {
                                        ScannerManager.AddEnergy((int)Event.Parameters[0].Parameter);
                                        WorldManager.AwardMessage("Energy Gained!", (int)Event.Parameters[0].Parameter, "+", string.Empty, new int[] { 33, 70, 255, 255 }, 0.001f);

                                        //World.StorageManager.SaveWorldInfo();

                                        break;
                                    }
                                case "AddBattery":
                                    {
                                        ScannerManager.AddBattery((int)Event.Parameters[0].Parameter);
                                        WorldManager.AwardMessage("Battery Gained!", (int)Event.Parameters[0].Parameter, "+", string.Empty, new int[] { 33, 70, 255, 255 }, 0.001f);

                                        //World.StorageManager.SaveWorldInfo();

                                        break;
                                    }
                                case "AddLives":
                                    {
                                        Util.Cache.Avatar.AddLives((int)Event.Parameters[0].Parameter);
                                        WorldManager.AwardMessage("Lives Gained!", (int)Event.Parameters[0].Parameter, "+", string.Empty, new int[] { 33, 70, 255, 255 }, 0.001f);

                                        //World.StorageManager.SaveWorldInfo();

                                        break;
                                    }
                                  
                            }

                            break;
                        }
                    case Util.Helpers.PublicEnums.EventType.onItemUsed:
                        {
                            switch (Event.Method)
                            {
                                case "ThrowItem":
                                    {
                                        //declare
                                        Util.Classes.Item item;

                                        item=(Util.Classes.Item)Event.Parameters[0].Parameter;

                                        //init fields
                                        World.WorldManager.ThrowDirectionChosen = false;
                                        item.ThrowDirection = Util.Helpers.PublicEnums.ItemMoveDirection.None;

                                        item.ThrowItem();

                                        break;
                                    }
                            }
                            break;
                        }
                   

                }




            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }


        }


        /// <summary>
        /// Get event from a list of events 
        /// by event type
        /// </summary>
        /// <param name="EventType"></param>
        /// <returns></returns>
        public static Util.Classes.Event GetEventByType(Util.Helpers.PublicEnums.EventType eventType, List<Util.Classes.Event> listOfEvents)
        {
            //declare
            Util.Classes.Event foundEvent = null; 


            try
            {
                for (int i = 0; i < listOfEvents.Count; i++)
                {
                    if (listOfEvents[i].EventType == eventType)
                    {
                        foundEvent = listOfEvents[i];
                        break;
                    }
                }




                return foundEvent;
            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return null;
            }
        }

        #endregion
    }
}
